package com.ckm.controller;

import com.ckm.annotation.LogInfo;
import com.ckm.entity.Classes;
import com.ckm.entity.ResponseResult;
import com.ckm.service.ClassesService;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@LogInfo
@Validated
@RestController
@RequestMapping("classes")
public class ClassesController {

    @Autowired
    private ClassesService classesService;

    /**
     * 添加班级
     * @param classesList 班级信息list
     * @return 成功信息
     */
    @PostMapping("addClasses")
    public ResponseResult addClasses(@RequestBody @Valid List<Classes> classesList) {
        classesService.addClasses(classesList);
        return new ResponseResult(200,"添加成功",null);
    }

    /**
     * 更新班级
     * @param classes 更新的信息
     * @return 成功信息
     */
    @PutMapping
    public ResponseResult updateClasses(@RequestBody Classes classes) {
        classesService.updateClasses(classes);
        return new ResponseResult(200,"更新成功","");
    }

    /**
     * 删除班级
     * @param id 班级Id
     * @return 成功信息
     */
    @DeleteMapping
    public ResponseResult deleteClasses(@RequestParam(name = "id") Integer id) {
        classesService.deleteClasses(id);
        return new ResponseResult(200,"删除成功","");
    }

    /**
     * 查找班级信息
     * @param classes 查找词
     * @param pageNum 页码
     * @param pageSize 页大小
     * @return 分页结果集
     */
    @PostMapping("find")
    public ResponseResult findClasses(@RequestBody Classes classes, @RequestParam(name = "pageNum",defaultValue = "1") int pageNum, @RequestParam(name = "pageSize", defaultValue = "10") int pageSize) {
        return new ResponseResult(200,"查找完毕",classesService.getClassesPage(classes,pageNum,pageSize));
    }

    /**
     * 查找需要进行某课程的所有班级
     * @param courseName 课程名称
     * @return 班级集合
     */
    @GetMapping("byCourse")
    public ResponseResult findClassesByCourse(@RequestParam(name = "courseName") String courseName){
        return new ResponseResult(200,"上"+courseName+"的班级",classesService.findClassesByCourse(courseName));
    }

    /**
     * 拼接班级名称
     * @param id 班级id
     * @return 拼接好的班级名称
     */
    @GetMapping("{id}")
    public ResponseResult getClassesName(@PathVariable("id") Integer id) {
        return new ResponseResult(200,"班级id："+id+"的班级名称",classesService.concatenateClassName(id));
    }
}
